package com.example.demo;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author: wuhong-016
 * @time: 2021/4/9 14:15
 * @version: v1.0.0
 * @description:映射webapp下面的不同文件夹
 */
public class Mapper {

    private static Map<String, String> mapper = new HashMap<>();
    private static Map<String, String> projects = new HashMap<>();

    public static void doInit(String file) throws IOException {
        List<String> list = TxtUtil.readTxtByPage(file, 1, 1000);
        list.forEach(s -> {
            if (null != s && s.contains("=")) {
                mapper.put(s.split("=")[0], s.split("=")[1]);
            }
        });

        doScan();
    }

    private static void doScan() {
        String path = mapper.get("server.service.engine.host.appBase");
        File dir = new File(path);
        if (dir.isDirectory()) {
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    projects.put(file.getName(), file.getPath());
                }
            }
        } else {
            throw new IllegalArgumentException("参数错误，server.service.engine.host.appBase应当为一个文件夹！");
        }
    }

    public static String get(String key) {
        return mapper.get(key);
    }

    public static Map<String, String> getProjects() {
        return projects;
    }

    public static void main(String[] args) throws IOException {
        String file = "E:\\workspace\\example\\minicat\\src\\main\\resources\\server.txt";
        doInit(file);
        System.out.println(mapper.get("server.service.engine.host.name"));
    }

}
